<template>
  <view>
    <!-- 轮播图-->
    <swiper indicator-dots autoplay :interval="3000" :duration="1000">
      <swiper-item v-for="(item,i) in goods_info.pics" :key="i">
        <image :src="item.pics_big" @click="preview(i)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息-->
    <view class="goods-info-box">
      <view class="goods-price">
        ￥ {{goods_info.goods_price | tofixed}}
      </view>
      <view class="goods-info-body">
        <view class="goods-name">
          {{goods_info.goods_name}}
        </view>
        <view :class="goods_info.goods_state!==2?'goods_state1':'goods_state2'">
          <uni-icons type="star" size="18"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
    </view>
    <!-- 运费-->
    <view class="goods_freight">
      快递 : 免运费
    </view>
    <!-- 商品详情-->
    <view class="goods_introduce">
      <view class="goods_introduce-title">
        / * 商品详情 * /
      </view>
      <view class="goods_introduce-item" v-html="goods_info.goods_introduce"></view>
    </view>
    <!-- 商品导航-->
    <view class="goods_nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick"></uni-goods-nav>
    </view>
  </view>
</template>

<script>
  import {
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    data() {
      return {
        goods_info: {},
        options: [{
          icon: 'shop',
          text: '店铺',
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    computed: {
      ...mapGetters('m_cart', ['total'])
    },
    onLoad(options) {
      const {
        goods_id
      } = options
      this.getGoodsList(goods_id)
    },
    filters: {
      // 把数字处理为带两位小数点的数字
      tofixed(num) {
        return Number(num).toFixed(2)
      }
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      async getGoodsList(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img/g, '<img style="display:block;"')
        this.goods_info = res.message
        // console.log(this.goods_info)
      },
      onClick(e) {
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      buttonClick(e) {
        if (e.content.text === '加入购物车') {
          const {
            goods_id,
            goods_name,
            goods_price,
            goods_small_logo
          } = this.goods_info
          const goods = {
            goods_id,
            goods_name,
            goods_price,
            goods_count: 1,
            goods_small_logo,
            goods_state: true
          }
          this.addToCart(goods)
        } else {
          uni.switchTab({
            url:'/pages/cart/cart'
          })
        }
      },
      preview(i){
        // 使用uni.previewImage()进行图片浏览
        uni.previewImage({
          current:i,//浏览显示图片索引
          urls:this.goods_info.pics.map(e=>e.pics_big)
        })
        
        }
      },
        //监听total值的变化进行修改info的值
      watch:{
        total:{
          handler(newVal){
          //使用find进行数组中的寻找并返回
        const findTotal = this.options.find(e=>e.text==='购物车')
        //如果有,改变返回对象中的 info值
        // console.log(findTotal)
          findTotal.info = newVal
        },
        immediate:true
        }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 400rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-info-box {
    padding: 20rpx;
    padding-right: 0;

    .goods-price {
      color: #c00000;
      font-size: 36rpx;
      margin: 20rpx 0;
    }

    .goods-info-body {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 26rpx;
        padding-right: 20rpx;
      }

      .goods_state1 {
        width: 240rpx;
        font-size: 24rpx;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1rpx solid #efefef;
        color: gray;

        .uni-icons {
          color: gray !important;
        }
      }

      .goods_state2 {
        width: 240rpx;
        font-size: 24rpx;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1rpx solid #efefef;
        color: red;

        .uni-icons {
          color: red !important;
        }
      }
    }

  }

  .goods_freight {
    margin: 20rpx 20rpx;
    font-size: 24rpx;
    color: gray;
  }

  .goods_introduce-title {
    margin: 40rpx 0;
    text-align: center;
    color: #c00000;
  }

  .goods_nav {
    position: sticky;
    bottom: 0;
    left: 0;
    right: 0;
  }

  .goods_introduce {
    padding-bottom: 50rpx;
  }
</style>
